/*
 Copyright (c) 2009 Rafal Szemraj - http://szemraj.eu

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 of the Software, and to permit persons to whom the Software is furnished to do so,
 subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all 
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/
package eu.szemraj.logging.channel {
	import eu.szemraj.managers.SingletonManager;   

	import flash.events.SecurityErrorEvent;	import flash.events.StatusEvent;	import flash.net.LocalConnection;
	import eu.szemraj.logging.JSONEncoderImplementation;	import eu.szemraj.logging.LogLevel;   

	public class DevLoggerChannel implements ILogChannel {

		private static var _instance:DevLoggerChannel;
		private var lc:LocalConnection;

		
		public static function getInstance():DevLoggerChannel {
			
			return _instance ? _instance : _instance = new DevLoggerChannel();
		}

		public function DevLoggerChannel() {
            
			SingletonManager.checkSingleton(this);
			lc = new LocalConnection();
			lc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError, false, 0, true);
			lc.addEventListener(StatusEvent.STATUS, onStatusEvent, false, 0, true);
		}

		/**
		 * Logs given message at given log level, loggingObject is threaten as source of message
		 * @param message messafge to log
		 * @param logLevel level at wich message will be logged
		 * @param loggingObject source of message <strong>[ optional ]</strong>
		 */
		public function log(message:*, logLevel:LogLevel, loggingObject:String ):void {
            
			lc.send("_airDebuggerConnection", "logMessage", "" + loggingObject + " : " + message, logLevel.getName());
		}

		/**
		 * Inspects object ( eg. shows its structure in tree, datagrid etc ). How this method works
		 * depends on log channel inspectObject method implementation. <b>Important</b> - remember, it shows only dynamic
		 * properties of object. To show whole information use as3 flash.utils.describeType.
		 * @param object object to inspect
		 * @param objectName name of object. You can pass any string that distinguish current object from others,
		 * if ommit "object" is set. <strong>[ optional ]</strong>
		 * @see Logger
		 */
		public function inspectObject(object:*, objectName:String = "object" ):void {
            
			lc.send("_airDebuggerConnection", "inspectObject", ( new JSONEncoderImplementation(object) ).getString(), objectName);
		}

		/**
		 * Clears channel's output.
		 */
		public function clearOutput():void {
		}

		private function onSecurityError( event:SecurityErrorEvent ):void {
		}

		private function onStatusEvent( event:StatusEvent ):void {
		}
	}
}

